import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ffmeg1main {
    public static void main(String args[]) throws Exception {

        String infile = "E:\\testResource\\琥珀蜜蜡.mp4";
        String outfile = "E:\\testResource\\err123.mp4";
        ffmeg1main  ff =new ffmeg1main();
        //outfile文件已存在，转码会卡死
        if(ff.transfer(infile,outfile)) {
            System.out.println("the transfer is ok!");
        } else {
            System.out.println("the transfer is error!");
        }
    }

    //格式:"00:00:10.68"
    private static int getTimelen(String timelen){
        int min=0;
        String strs[] = timelen.split(":");
        if (strs[0].compareTo("0") > 0) {
            min+=Integer.valueOf(strs[0])*60*60;//秒
        }
        if(strs[1].compareTo("0")>0){
            min+=Integer.valueOf(strs[1])*60;
        }
        if(strs[2].compareTo("0")>0){
            min+=Math.round(Float.valueOf(strs[2]));
        }
        return min;
    }

    public boolean transfer(String infile,String outfile) {
        StringBuffer sb = new StringBuffer();

        //ffmpeg命令格式：ffmpeg {全局参数} {输入文件参数} -i {输入文件} {输出文件参数} {输出文件}
        //视频转码
        String avitoflv = "E:\\testResource\\FFmpeg\\ffmpeg -i "+infile+" -s 320x240 "+outfile;
//        String avitoflv = "ffmpeg -i "+infile+" -ar 22050 -ab 56 -f flv -y -s 320x240 "+outfile;
//        String flvto3gp = "ffmpeg -i " + infile + " -ar 8000 -ac 1 -acodec amr_nb -vcodec h263 -s 176x144 -r 12 -b 30 -ab 12 " + outfile;
//        String avito3gp = "ffmpeg -i " + infile + " -ar 8000 -ac 1 -acodec amr_nb -vcodec h263 -s 176x144 -r 12 -b 30 -ab 12 " + outfile;
//        String avitojpg = "ffmpeg -i " + infile + " -y -f image2 -ss 00:00:10 -t 00:00:01 -s 350x240 " + outfile;
        //视频切片
        String segment="E:\\testResource\\FFmpeg\\ffmpeg -i "+infile+" -f segment -segment_time 60 -segment_format mpegts -segment_list E:\\testResource\\video-folder\\hpml.m3u8 -c copy -bsf:v h264_mp4toannexb -map 0 E:\\testResource\\video-folder\\course-%04d.ts";

        String encryp="E:\\testResource\\FFmpeg\\ffmpeg -i "+infile+" -c copy -bsf:v h264_mp4toannexb -hls_time 10 -hls_key_info_file E:\\testResource\\enc.key -hls_segment_filename \"E:\\testResource\\file-folder\\file%d.ts\" playlist.m3u8";

        String encryp1="E:\\testResource\\FFmpeg\\ffmpeg -y -i "+infile+" -hls_time 60 -hls_key_info_file E:\\testResource\\enc.keyinfo -hls_playlist_type vod -hls_segment_filename \"E:\\testResource\\file-folder\\file%5d.ts\" E:\\testResource\\file-folder\\video.m3u8";

        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(encryp1);
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;

            while ( (line = br.readLine()) != null) {
                System.out.println(line);
                sb.append(line);
            }
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);

            //视频信息
            String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
            Pattern pattern = Pattern.compile(regexDuration);
            Matcher m = pattern.matcher(sb.toString());
            if (m.find()) {
                int time = getTimelen(m.group(1));
                System.out.println("视频信息====》时长："+time+"秒, 开始时间："+m.group(2)+",比特率："+m.group(3)+"kb/s");
            }
            String regexVideo ="Video: (.*?), (.*?), (.*?)[,\\s]";
            pattern = Pattern.compile(regexVideo);
            m = pattern.matcher(sb.toString());
            if (m.find()) {
                System.out.println("视频信息====》编码格式："+m.group(1)+", 视频格式："+m.group(2)+",分辨率："+m.group(3));
            }

            String regexAudio ="Audio: (.*?), (\\d*) Hz";
            pattern = Pattern.compile(regexAudio);
            m = pattern.matcher(sb.toString());
            if (m.find()) {
                System.out.println("视频信息====》音频编码："+m.group(1)+", 音频采样频率："+m.group(2)+" Hz");
            }
        } catch (Throwable t) {
            t.printStackTrace();
            return false;
        }
        return true;
    }

}
